# -*- coding: utf-8 -*-
"""
Created on Wed Mar 22 09:31:03 2023

@author: skunk69
"""

from pathlib import Path,PurePath

# 拼接出Windows桌面路径
path1 = Path(Path.home(), "Desktop")
print('the path is {}'.format(path1.absolute()))

# 拼接出Windows桌面路径
path2 = Path.joinpath(Path.home(), "Desktop")
print(f'the path is {path2.absolute()}')

path3 = Path.cwd() / 'test'
print(f'the path is {path3.absolute()}')

path4 = PurePath(Path.cwd(),'test','jax')
print('the path is {}'.format(path4))

path5 = Path('test')
print('the path is {}'.format(path5.absolute()))

input_path = r"C:\Users\skunk69\Pictures"
path6 = Path(input_path)
print('the path is {}'.format(path6.absolute()))
print('the path name is {}'.format(path6.name))
print('the path suffix is {}'.format(path6.suffix))
print('the path suffixes is {}'.format(path6.suffixes))
print('the path root is {}'.format(path6.root))
print('the path parts is {}'.format(path6.parts))
print('the path anchor is {}'.format(path6.anchor))
print('the path parent is {}'.format(path6.parent))
print('the path parents is {}'.format(path6.parents))

input_path = PurePath(Path.cwd(),'testGradf.py')
# input_path = PurePath(Path.cwd())
if Path(input_path).exists():
	if Path(input_path).is_file():
		print("是文件！")
	elif Path(input_path).is_dir():
		print("是文件夹！")
else:
	print("路径有误，请检查!")

# 创建文件夹函数
def creat_dir(dir_path):
	if Path(dir_path).exists():  # 如果已经存在，则跳过并提示
		print("文件夹已经存在！")
	else:
		Path.mkdir(Path(dir_path))  # 创建文件夹

input_path = PurePath(Path.cwd(),'test')
print('create the path {}'.format(input_path))
creat_dir(input_path )  # 调用函数
print('the path {} exists ? {}'.format(input_path,Path(input_path).exists()))

# 删除文件夹函数
def del_dir(dir_path):
	if Path(dir_path).exists():  # 如果存在，则删除文件夹
		Path.rmdir(Path(dir_path))
	else:
		print("文件夹不存在！")  # 文件夹不存在
print('remove the path {}'.format(input_path))
del_dir(input_path)  # 调用函数
print('the path {} exists ? {}'.format(input_path,Path(input_path).exists()))

# 删除文件函数
def del_file(dir_path):
	if Path(dir_path).exists():  # 如果存在，则删除文件
		Path.unlink(Path(dir_path))
	else:
		print("文件不存在！")  # 文件不存在
input_path = PurePath(Path.cwd(),'test.py')
del_file(input_path)  # 调用函数

# 路径匹配查找
# 获取文件夹下所有文件，返回文件路径（字符）列表，采用glog方法
input_path = Path.cwd()
files = [str(f) for f in Path(input_path).glob(f"*.*") if Path(f).is_file()]
print('the files are {}'.format(files))
print('the files number are {}'.format(len(files)))

# 获取文件夹下所有文件类型，返回文件后缀的列表，采用glog方法
suffixs = list(set([Path(f).suffix for f in Path(input_path).glob(f"*.*") if Path(f).is_file()]))
print('the suffixs are {}'.format(suffixs))

# 获取文件夹下（含子文件）所有文件，返回文件路径(字符)列表，采用rglog方法
files = [str(f) for f in Path(input_path).rglob(f"*.*") if Path(f).is_file()]
print('the files (and subfolders) are {}'.format(files))
print('the files number are {}'.format(len(files)))

# 获取文件夹下（含子文件）所有文件类型，返回文件后缀的列表，采用rglog方法
suffixs = list(set([Path(f).suffix for f in Path(input_path).rglob(f"*.*") if Path(f).is_file()]))
print('the suffixs are {}'.format(suffixs))

# 读写文件
input_path = PurePath(Path.cwd(),'test.txt')

with Path(input_path).open('w') as f:  # 创建并打开文件
	f.write('测试:读写文件')  # 写入内容
print(f'the path {input_path} exists ? {Path(input_path).exists()}')

with Path(input_path).open('r') as f: 
    print(f"读取的文件内容为：{f.read()}")

print('remove the path {}'.format(input_path))
del_file(input_path)
print(f'the path {input_path} exists ? {Path(input_path).exists()}')

